package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfBdPlatfromRela;
import com.jic.point.model.entity.ConfBdPlatfromRelaApply;
import com.jic.point.model.vo.request.ConfBdPlatfromRelaApplyRequest;
import com.jic.point.model.vo.response.ConfBdPlatfromRelaApplyResponse;
import com.jic.point.service.ConfBdPlatfromRelaApplyService;
import com.jic.point.service.ConfBdPlatfromRelaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: admin
 * @date: 2020-11-27 14:44:44
 */
 @RestController
 @Slf4j
 @RequestMapping("/confBdPlatfromRelaApply")
 @Api(tags = " [BD部门和平台关联申请表]控制类-PC")
public class ConfBdPlatfromRelaApplyController {
	@Autowired
	private ConfBdPlatfromRelaApplyService service;

    @Autowired
    private ConfBdPlatfromRelaService relaService;
     /**
      *新增记录
      * @param request 新增[BD部门和平台关联申请表]请求参数
      * @return ConfBdPlatfromRelaApplyResponse 新增[BD部门和平台关联申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[BD部门和平台关联申请表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
     		ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
    		BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
    	    long flag = service.save(confBdPlatfromRelaApply);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[BD部门和平台关联申请表]请求参数
      * @return ConfBdPlatfromRelaApplyResponse 新增或修改[BD部门和平台关联申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[BD部门和平台关联申请表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
      		ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
		     BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
	         long flag =   service.saveOrUpdate(confBdPlatfromRelaApply);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[BD部门和平台关联申请表]请求参数
      * @return ConfBdPlatfromRelaApplyResponse 删除[BD部门和平台关联申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[BD部门和平台关联申请表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
      		ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
	     	BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
	         long flag = service.deleteByPrimaryKeyLogically(confBdPlatfromRelaApply);
      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[BD部门和平台关联申请表]请求参数
      * @return ConfBdPlatfromRelaApplyResponse 修改[BD部门和平台关联申请表]响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[BD部门和平台关联申请表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
      		ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
	     	BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
	         long flag = service.updateByPrimaryKeySelective(confBdPlatfromRelaApply);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[BD部门和平台关联申请表]详情请求参数
      * @return ConfBdPlatfromRelaApplyResponse 查询[BD部门和平台关联申请表]详情响应参数
      * author admin
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[BD部门和平台关联申请表]详情")
      @ResponseBody
     public RestResult<ConfBdPlatfromRelaApplyResponse> getByPrimaryKey(@Validated  @RequestBody ConfBdPlatfromRelaApplyRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
	     	BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
	     	ConfBdPlatfromRelaApply result = service.getByPrimaryKey(confBdPlatfromRelaApply);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfBdPlatfromRelaApplyResponse bo = new ConfBdPlatfromRelaApplyResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfBdPlatfromRelaApplyResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfBdPlatfromRelaApply>> listPage (@RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
      ConfBdPlatfromRelaApply param = new ConfBdPlatfromRelaApply();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_BD_PLATFROM_RELA_APPLY 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [BD部门和平台关联申请表] ")
      @ResponseBody
      public RestResult<List<ConfBdPlatfromRelaApplyResponse>>  listConfBdPlatfromRelaApplyAllRecord(@RequestBody ConfBdPlatfromRelaApplyRequest request) throws Throwable {
      		ConfBdPlatfromRelaApply param = new ConfBdPlatfromRelaApply();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfBdPlatfromRelaApply> list = service.listAllRecord(param);
			List<ConfBdPlatfromRelaApplyResponse> responseList = list.stream().map(temp -> {
			ConfBdPlatfromRelaApplyResponse bo = new ConfBdPlatfromRelaApplyResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }


    /**
     * 审核提交
     * @param request
     * @return
     * @throws Throwable
     */
    @PostMapping("/auditAubmit")
    @ApiOperation(value = "审核提交 [第三方机构主表] ")
    @ResponseBody
    public RestResult<Long> auditAubmit(@RequestBody ConfBdPlatfromRelaApplyRequest request){
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfBdPlatfromRelaApply confBdPlatfromRelaApply = new ConfBdPlatfromRelaApply();
        BeanUtils.copyProperties(request, confBdPlatfromRelaApply);
        ConfBdPlatfromRelaApply result = service.getByPrimaryKey(confBdPlatfromRelaApply);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditReason(request.getAuditReason());
        //审核成功
        if (result.getAuditSts() == 1) {
            //审核成功------新增
            if (result.getCurrentAction() == 0) {
                //申请表逻辑删除
                result.setAuditorId(request.getAdminOperate().getOperator());
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表中
                ConfBdPlatfromRela confBdPlatfromRela = new  ConfBdPlatfromRela();
                BeanUtils.copyProperties(result, confBdPlatfromRela);
                confBdPlatfromRela.setId(null);
                confBdPlatfromRela.setDeleteFlag(0);
                confBdPlatfromRela.setAuditSts(1);
                long flag = relaService.insertSelective(confBdPlatfromRela);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 1) {
                //审核成功-------编辑-------申请表逻辑删除
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //主表，通过修改code--编辑逻辑删除为0
                ConfBdPlatfromRela confBdPlatfromRela = new  ConfBdPlatfromRela();
                BeanUtils.copyProperties(result, confBdPlatfromRela);
                confBdPlatfromRela.setDeleteFlag(0);
                confBdPlatfromRela.setAuditSts(1);
                //confThirdPart.setAuditorId(result.getAdminOperate().getOperator());
                long flag = relaService.saveOrUpdateApply(confBdPlatfromRela);
                return RestResult.success(flag);
            } else {
                //审核成功--------删除---申请表删除
                result.setAuditorId(request.getAdminOperate().getOperator());
                result.setAuditSts(1);
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            }
        }else if(result.getAuditSts() == 2){
            //审核失败------新增失败
            if (result.getCurrentAction() == 0) {
                //申请表逻辑删除
                 result.setAuditorId(request.getAdminOperate().getOperator());
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 1 || result.getCurrentAction() == 2) {
                //删除、编辑失败，申请表逻辑删除，主表逻辑删除改为1
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                ConfBdPlatfromRela confBdPlatfromRela = new  ConfBdPlatfromRela();
                BeanUtils.copyProperties(result, confBdPlatfromRela);
                confBdPlatfromRela.setDeleteFlag(0);
                confBdPlatfromRela.setAuditSts(2);
                long flag = relaService.saveOrUpdateApply(confBdPlatfromRela);
                return RestResult.success(flag);
            }
        }else{
            result.setAuditSts(3);
            long flag = service.deleteByPrimaryKeyLogically(result);
            return RestResult.success(flag);
        }
        return null;
    }
     }

	
